<template>
	<view class="content">
		<view class="logo-wrap">
			<image :src="icon" class="logo" mode="widthFix"></image>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				status: null, //区分活动
				icon: this.$imgUrl + 'index/logo.png',
			}
		},
		onLoad(options) {

			if (options.status) {
				//common
				let data = {

				};

				//different
				let status = options.status;
				this.status = status;
				switch (status) {
					case '6':
						//h5活动通用
						data.member = options.member;
						data.card_id = options.cardId;
						data.fee = options.fee * 100;
						data.cont = options.cont;
						break;
					case '3':
						//充值送豪礼活动h5
						data.member = 21;
						data.card_id = options.cardId;
						data.fee = options.fee * 100;
						data.cont = options.cont;
						break;
					case '1':
						//话费充值活动
						data.member = 23;
						data.phone = options.phone;
						data.fee = options.fee;
						break;
				};
				if (status) {
					this.paymentRequest(data);
				} else {
					this.$utils.toast('当前活动已取消，请您关注其它类似活动');
				};
			};
		},
		methods: {
			paymentRequest(data) {
				//1.wechat pay：
				uni.login({
					provider: 'weixin',
					success: ret => {
						data.code = ret.code; //增加code参数
						//create order
						this.$u.api.commonPay(data).then(res => {
							if (res.result_code == '314') {
								let resOrderId = res.data.order_id;
								let tradeNO = res.data.trade_no; //支付宝支付需要的tradeNO
								if (resOrderId) {

									uni.requestPayment({
										//微信
										// #ifdef	MP-WEIXIN
										provider: 'wxpay',
										timeStamp: res.data.timeStamp,
										nonceStr: res.data.nonceStr,
										package: res.data.package,
										signType: 'MD5',
										paySign: res.data.paySign,
										// #endif
										//支付宝
										// #ifdef	MP-ALIPAY
										provider: 'alipay',
										orderInfo: tradeNO,
										// #endif
										success: (resultData) => {


											// #ifdef	MP-ALIPAY
											if (resultData?.resultCode == 6001 ||
												resultData?.resultCode == 6002) {
												//Payment failed
												this.$utils.modal({
													content: '支付失败！',
													showCancel: false,
													callback: (bool) => {
														this.$utils.back();
													}
												});
												return false;
											};
											// #endif

											//Check if payment is successful
											this.$u.api.checkPaymentSuccess({
												order_id: resOrderId,

											}).then((retu) => {
												if (retu.result_code == '300') {
													if (retu.data.url) {
														uni.redirectTo({
															url: "/pages/webview/index?url=" +retu.data
																.url
																
														})
													} else {
														if (this.status == '1') {
															//话费充值成功后跳转至h5成功页面
															uni.redirectTo({
																url: "/pages/webview/index?url=https://api.cd1a.cn/h5/libo/phone/progress.html"
															})
														} else {
															this.$utils.back();

														};
													}
													//payment successful

												} else {
													this.$utils.modal({
														title: '提示',
														content: retu.msg,
														showCancel: false,

													});
												};
											}).catch(err => {});

										},
										fail: () => {
											//Payment failed
											this.$utils.modal({
												content: '支付失败！',
												showCancel: false,
												callback: (bool) => {
													this.$utils.back();
												}
											});
										}
									});
								};
							} else {
								this.$utils.modal({
									title: '提示',
									content: res.msg,
									showCancel: false,
									callback: (bool) => {
										this.$utils.back();
									}
								});
							};
						}).catch(() => {});

					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 100%;
		background-color: #0DA297;
	}

	.logo-wrap {
		padding-top: 40rpx;
		text-align: center;
	}

	.logo {
		width: 300rpx;
		vertical-align: middle;
	}
</style>
<style lang="scss">
	page {
		height: 100%;
	}
</style>